package com.dingreading.cloud.admin.util.bean;


import com.dingreading.cloud.admin.dto.CmservicesDto;
import com.dingreading.cloud.admin.entity.Cmorders;
import com.dingreading.cloud.admin.entity.Cmservices;
import com.dingreading.cloud.admin.service.CaCallNamesService;
import com.dingreading.cloud.common.util.EnumType;
import com.dingreading.cloud.common.util.KitUtil;
import com.dingreading.cloud.common.util.NepUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 会员订单工具
 */
@Component
public class CmorderUtil {

    @Resource
    private CaCallNamesService caCallNameService;


    public static CmorderUtil util;

    @PostConstruct
    public void init() {
        util = this;
    }

    private static final int openCardStatus = EnumType.ServiceStatus.OPEN_CARD.getStatus();
    private static final int pauseCardStatus = EnumType.ServiceStatus.PAUSE_CARD.getStatus();

    /**
     * 新建会员服务订单
     */
    public static Cmorders newCmorder(
            Cmorders data, String corpUid, String storeUid, String storeName, String staffUid, String staffName, String memberUid, String memberName,
            String saleStaffUid, String saleStaffFullName, String saleStaffPriPhone, String saleStaffPubPhone, String saleStaffNickName,
            String contractCode, String orderType, Double orderAmount, Date orderDate,
            String giftGotten, String giftRemark, String orderRemark, String payChannelCode, Integer serviceNum,String source
    ) {
        if (null == data) {
            data = new Cmorders();
            data.setCorpUid(corpUid);
            data.setStoreUid(storeUid);
            data.setStoreName(storeName);
            data.setAddStaffUid(staffUid);
            data.setAddStaffName(staffName);
            data.setOrderCode(KitUtil.getMemberServiceCode());
            data.setMemberUid(memberUid);
            data.setMemberName(memberName);
            data.setStatus(0);
            data.setStatusName("待审批");
            // 正常
            data.setInoutType(1);
            data.setInoutTypeName("收入");
            data.setOrderState(1);
            data.setOrderStateName("正常");
        }
        data.setOrderType(NepUtil.nullToZero(orderType));
        data.setContractCode(NepUtil.nullToZero(contractCode));
        data.setOrderAmount(NepUtil.nullToZero(orderAmount));
        data.setGiftGotten(NepUtil.nullToZero(giftGotten));
        data.setGiftRemark(NepUtil.nullToZero(giftRemark));
        data.setOrderRemark(NepUtil.nullToZero(orderRemark));
        data.setPayChannelCode(NepUtil.nullToZero(payChannelCode));
        data.setOrderDate(orderDate);
        data.setServiceNum(NepUtil.nullToZero(serviceNum));

        data.setSaleStaffUid(NepUtil.nullToZero(saleStaffUid));
        data.setSaleStaffFullName(NepUtil.nullToZero(saleStaffFullName));
        data.setSaleStaffPriPhone(NepUtil.nullToZero(saleStaffPriPhone));
        data.setSaleStaffPubPhone(NepUtil.nullToZero(saleStaffPubPhone));
        data.setSaleStaffNickName(NepUtil.nullToZero(saleStaffNickName));

        data.setSource(source);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);

        return data;
    }

    /**
     * 会员订单服务详情
     */
    public static Cmservices newImportService(
            String corpUid, String storeUid, String storeName,
            String memberUid, String memberName, String staffUid, String staffName,
            String orderCode, String orderType, Integer totalLessonNum, Integer serviceDays, Integer isGift,
            Long projectId, String projectUid, String projectName, Integer lessonStatus, Integer freeStatus
    ) {
        Cmservices data = new Cmservices();
        data.setCorpUid(corpUid);
        data.setStoreUid(storeUid);
        data.setStoreName(storeName);
        data.setFromStoreUid(storeUid);
        data.setFromStoreName(storeName);
        data.setMemberUid(memberUid);
        data.setMemberName(memberName);
        data.setAddStaffUid(staffUid);
        data.setAddStaffName(staffName);
        data.setLastStaffUid(staffUid);
        data.setLastStaffName(staffName);

        data.setUid(KitUtil.getUuid());
        data.setOrderCode(orderCode);
        data.setOrderType(orderType);
        data.setStatus(EnumType.ServiceStatus.NO_CARD.getStatus());
        data.setStatusName(EnumType.ServiceStatus.NO_CARD.getName());
        data.setChangeStatus(EnumType.ServiceStatus.NO_CHANGE.getStatus());
        data.setIsGift(isGift);
        data.setEnding(0);
        data.setEndingType(0);
        data.setEndingTime(null);
        data.setEndingRemark(null);

        data.setServiceDays(serviceDays);
        data.setTotalLessons(totalLessonNum);
        data.setLeftLessons(totalLessonNum);
        if (data.getTotalLessons() != null)
            data.setLeftLessons(data.getTotalLessons());
        if (data.getServiceDays() != null)
            data.setLeftDays(data.getServiceDays());

        data.setLessonStatus(lessonStatus);
        data.setFreeStatus(freeStatus);
        data.setProjectUid(projectUid);
        data.setProductCode(projectId + "");
        data.setProductName(projectName);

        return data;
    }

    /**
     * 判断是否能排课且消耗课次
     */
    public static boolean isConsumeLesson(Integer lessonStatus, Integer freeStatus) {
        return lessonStatus.equals(1) && freeStatus.equals(1);
    }

    /**
     * 获取服务的名称+服务天数+活动次数
     */
    public static String getProductName(CmservicesDto service) {
        if (isConsumeLesson(service.getLessonStatus(), service.getFreeStatus())) {
            return service.getProductName() + "-" + service.getServiceDays() + "天" + "-" + service.getTotalLessons() + "次";
        } else {
            return service.getProductName() + "-" + service.getServiceDays() + "天";
        }
    }

    /**
     * 获取服务的名称+服务天数+活动次数
     */
    public static String getProductName(Cmservices service) {
        if (isConsumeLesson(service.getLessonStatus(), service.getFreeStatus())) {
            return service.getProductName() + "-" + service.getServiceDays() + "天" + "-" + service.getTotalLessons() + "次";
        } else {
            return service.getProductName() + "-" + service.getServiceDays() + "天";
        }
    }

    /**
     * 服务获取以活动次数，状态名称修改
     */
    public static void setUseLessonCntAndStatusName(
            List<CmservicesDto> serviceList, Map<String, Long> map
    ) {
        int openCardStatus = EnumType.ServiceStatus.OPEN_CARD.getStatus();
        int pauseCardStatus = EnumType.ServiceStatus.PAUSE_CARD.getStatus();
        for (CmservicesDto dto : serviceList) {
            if (isConsumeLesson(dto.getLessonStatus(), dto.getFreeStatus())) {
                // 设置已完结/已退费/已变更的状态名
                if (null != dto.getChangeStatus() && !dto.getChangeStatus().equals(EnumType.ServiceStatus.NO_CHANGE.getStatus())) {
                    String changeName = dto.getChangeStatusName() + (dto.getChangeValue() != null ? dto.getChangeValue() + "次" : "");
                    dto.setStatusName(changeName);
                }
                // 获取服务已活动的次数
                if (null != map && null != dto.getStatus()
                        && (dto.getStatus().equals(openCardStatus) || dto.getStatus().equals(pauseCardStatus)))
                    dto.setUseLessonCnt(NepUtil.nullToZero(map.get(dto.getUid())));
            } else {
                if (null != dto.getChangeStatus() && !dto.getChangeStatus().equals(EnumType.ServiceStatus.NO_CHANGE.getStatus())) {
                    String changeName = dto.getChangeStatusName() + (dto.getChangeValue() != null ? dto.getChangeValue() + "天" : "");
                    dto.setStatusName(changeName);
                }
            }
        }
    }

    /**
     * 已开卡且可排活动的服务uid集合，获取消耗数量
     */
    public static Map<String, Long> getConsumeLessonMapByDto(
            String storeUid, List<CmservicesDto> servicesList
    ) {
        // 已开卡、可排活动、消耗活动次数的服务uid集合，查询消课数量
        List<String> consumeUidList = servicesList.stream()
                .filter(service -> service.getLessonStatus().equals(1) && service.getFreeStatus().equals(1)
                        && service.getStatus() != null && (service.getStatus().equals(openCardStatus) || service.getStatus().equals(pauseCardStatus)))
                .map(CmservicesDto::getUid)
                .collect(Collectors.toList());

        Map<String, Long> consumeMap = new HashMap<>();
        if (CollectionUtils.isNotEmpty(consumeUidList))
            consumeMap = util.caCallNameService.getCntByServiceUids(storeUid, consumeUidList);
        return consumeMap;
    }

}
